﻿using Iworld.Backstage.Framework;
using Iworld.Backstage.Framework.AdministratorService;
using Iworld.Backstage.Framework.AuthorService;
using Iworld.Toolkit.Helper;
using Iworld.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Backstage
{
    [View(Page.用户列表)]
    public partial class UserListPage : UserControl
    {
        public UserListPage()
        {
            InitializeComponent();
        }

        private void Edit(object sender, RoutedEventArgs e)
        {
            AuthorExport ae = XamlHelper.GetDataContext<AuthorExport>(sender);
            EditUserWindow euw = new EditUserWindow();
            euw.Info = ae;
            euw.Show();
        }

        private void Remove(object sender, RoutedEventArgs e)
        {
            ConfirmPrompt cp = new ConfirmPrompt() { Info = "删除之后不可恢复,确认删除吗?" };
            cp.Closed += (s, a) =>
            {
                if (cp.DialogResult == true)
                {
                    AuthorExport ae = XamlHelper.GetDataContext<AuthorExport>(sender);
                    AuthorServiceClient client = new AuthorServiceClient();
                    client.RemoveUserCompleted += client_RemoveUserCompleted;
                    client.RemoveUserAsync(new RemoveUserImport()
                    {
                        UserId = ae.Id,
                        Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
                    });
                }
            };
            cp.Show();
        }

        void client_RemoveUserCompleted(object sender, RemoveUserCompletedEventArgs e)
        {
           if(e.Result.Success)
           {
               new ErrorPrompt() { Info = "移除成功" }.Show();
               UserListViewModel uv = this.DataContext as UserListViewModel;
               uv.RefreshCommand.Execute(1);
           }else
           {
               new ErrorPrompt() { Info = e.Result.Error}.Show();
           }
        }

    }
}
